<?php

namespace frontend\modules\marketing\models;

use Yii;
use yii\base\Exception;

/**
 * This is the model class for table "coupon".
 *
 * @property string $coupon_id
 * @property string $coupon_name
 * @property integer $start_time
 * @property integer $end_time
 * @property integer $coupon_number
 * @property string $coupon_money
 * @property string $coupon_mustmoney
 */
class Coupon extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'coupon';
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
//            TimestampBehavior::className(),
            // 名为myBehavior4的行为，也是给出了MyBehavior类的配置数组
            'uuid' => [
                'class' => \common\behavior\uuidBehavior::className(),
                'owner' => $this
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['coupon_id','default','value'=>$this->getUUID()],
            [['coupon_id','start_time','end_time', 'coupon_number','coupon_money', 'coupon_mustmoney','coupon_name'], 'required'],
            [['start_time', 'end_time', 'coupon_number'], 'integer'],
            [['coupon_money', 'coupon_mustmoney'], 'number'],
            [['coupon_id'], 'string', 'max' => 40],
            [['coupon_name'], 'string', 'max' => 60],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'coupon_id' => Yii::t('app', 'Coupon ID'),
            'coupon_name' => Yii::t('app', '优惠券名称'),
            'start_time' => Yii::t('app', '开始时间'),
            'end_time' => Yii::t('app', '结束时间'),
            'coupon_number' => Yii::t('app', '优惠券数量'),
            'coupon_money' => Yii::t('app', '减价'),
            'coupon_mustmoney' => Yii::t('app', '满价'),
        ];
    }

    /**
     * @inheritdoc
     * @return \frontend\modules\marketing\models\query\CouponQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new \frontend\modules\marketing\models\query\CouponQuery(get_called_class());
    }

    public function afterFind()
    {
        $this->end_time = date('Y-m-d',$this->end_time);
        $this->start_time = date('Y-m-d',$this->start_time);
    }

    /**
     * 生成订单，记录优惠券使用，已使用
     * @param $order_id
     * @param $postData
     * @throws Exception
     */
    public static function userCoupon($order_id,$postData)
    {
        $conpuonOrder = new CouponOrder;
        $conpuonOrder->order_id = $order_id;
        $conpuonOrder->coupon_id = $postData['coupon_id'];
        $conpuonOrder->coupon_user_id = $postData['coupon_user_id'];
        if (!$conpuonOrder->save()) {
            throw new Exception("优惠券使用失败！");
        }

        $CouponUser = CouponUser::findOne($postData['coupon_user_id']);
        $time = time();
        $end_time = \frontend\modules\marketing\models\Coupon::findOne($CouponUser->coupon_id)->end_time;
        if(strtotime($end_time)<$time) {
            throw new Exception('优惠券已过期!');
        }
        if($CouponUser->is_used==1){
            throw new Exception('优惠券已使用!');
        }
        $CouponUser->is_used = 1;
        if (!$CouponUser->save()) {
            throw new Exception("优惠券使用失败！");
        }
    }
}
